package com.lzc.lblog.lblog.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.lzc.lblog.lblog.vo.CommentVo;
import com.lzc.lblog.lblog.vo.PostVo;

import org.springframework.stereotype.Controller;

import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("post")
public class PostController extends BaseController{




    /**
     * :\\d*这个表示id只能是数字
     * 跳转到分类下的博客
     * @return
     */
    @RequestMapping("category/{cid:\\d*}")
    public String postCategory(@PathVariable("cid")Long cid,
                               @RequestParam(value = "pn",defaultValue = "1") Integer pn,
                               @RequestParam(value = "status",required = false)Integer status,
                               @RequestParam(value = "order",required = false,defaultValue = "created")String order){
        req.setAttribute("cid",cid);
        req.setAttribute("pn",pn);
        req.setAttribute("status",status);
        req.setAttribute("order",order);
        return "/post/category";
    }
    /**
     * 跳转到博客详情页
     * @return
     */
    @RequestMapping("detail/{id:\\d*}")
    public String postDetail(@PathVariable("id")Long id,
                             @RequestParam(value = "pn",defaultValue = "1")Long pn,
                             @RequestParam(value = "size",defaultValue = "2")Long size){
       PostVo postVo=mPostService.selectDetail(id);
        Assert.notNull(postVo,"文章已被删除");

        mPostService.viewCountIncr(postVo);

       req.setAttribute("post",postVo);
       //获取博客的评论
       IPage<CommentVo> commentVoList=mCommentService.selectListByPid(new Page(pn,size),postVo.getId(),null,"created");
       req.setAttribute("commentVoList",commentVoList);
        req.setAttribute("cid",postVo.getCategoryId());
        return "/post/detail";
    }
    /**
     * 跳转到添加博客页面
     * @return
     */
    @RequestMapping("add")
    public String postAdd(){
        return "/post/add";
    }
}
